package android.TaskApp;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class WebServiceHelper {
	
	static String NAMESPACE = "http://SimpleTaskService/";	
		
	public static SoapObject InvokeMethod(String URL,String MethodName)
    {
        SoapObject request = GetSoapObject(MethodName);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("userid");
        pi.setValue(2);
        pi.setType(int.class);
        request.addProperty(pi);
        SoapSerializationEnvelope envelope = GetEnvelope(request);
        return  MakeCall(URL,envelope,NAMESPACE,MethodName);
    }
	
	public static SoapObject InvokeMethod(String URL,String MethodName, PropertyInfo[] pi)
    {
        SoapObject request = GetSoapObject(MethodName);
        
        for(int i=0; i < pi.length; i++)
        	request.addProperty(pi[i]);
        
        SoapSerializationEnvelope envelope = GetEnvelope(request);
        return  MakeCall(URL,envelope,NAMESPACE,MethodName);
    }

    public static SoapObject GetSoapObject(String MethodName)
    {
        return new SoapObject(NAMESPACE,MethodName);
    }
    
    public static SoapSerializationEnvelope GetEnvelope(SoapObject Soap)
    {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //envelope.dotNet = true;
        envelope.setOutputSoapObject(Soap);
        return envelope;
    }
    
    /**
     * 
     * @param URL - The complete URL where the web service resides 
     * @param Envelope - The envelope to be passed
     * @param NAMESPACE - The web method namespace
     * @param METHOD_NAME - The method name
     * @return - SoapObject containing the resultset
     */
    public static SoapObject MakeCall(String URL, SoapSerializationEnvelope Envelope, String NAMESPACE, String METHOD_NAME)
    {
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
         try
            {
                androidHttpTransport.call(NAMESPACE + METHOD_NAME, Envelope);
                SoapObject response = (SoapObject)Envelope.getResponse();
                return response;
            }
         catch(Exception e)
         {
             e.printStackTrace();
             
         }
         return null;
    }
    
    
    public static UserResponse RetrieveUsersFromSoap(SoapObject soap)
    {
    	UserResponse userResponse = new UserResponse();
    	
    	SoapObject soapItems = (SoapObject)soap.getProperty("users");
    	UserVO[] users = new UserVO[soapItems.getPropertyCount()];
        for (int i = 0; i < users.length; i++) {
        	SoapObject pii = (SoapObject)soapItems.getProperty(i);           
        	UserVO user = new UserVO();            
        	user.setUserId(Integer.parseInt(pii.getProperty("userId").toString()));        	
        	user.setUserEmail(pii.getProperty("userEmail").toString());
        	user.setUserPassword(pii.getProperty("userPassword").toString());
        	user.setFirstName(pii.getProperty("firstName").toString());
        	user.setLastName(pii.getProperty("lastName").toString());
        	user.setLastLogin(pii.getProperty("lastLogin").toString());
        	users[i] = user;
        }
        
        int iStatusCode = Integer.parseInt(soap.getProperty("statusCode").toString());
        String sMessage = soap.getProperty("statusMessage").toString();
        
        userResponse.setStatusCode(iStatusCode);
        userResponse.setStatusMessage(sMessage);
        userResponse.setUsers(users);
                
        return userResponse;
    }
    
    public static UserGroupResponse RetrieveUserGroupsFromSoap(SoapObject soap)
    {
    	UserGroupResponse response = new UserGroupResponse();
    	
    	SoapObject soapItems = (SoapObject)soap.getProperty("usergroups");
    	UserGroupVO[] usergroups = new UserGroupVO[soapItems.getPropertyCount()];
        for (int i = 0; i < usergroups.length; i++) {
        	SoapObject pii = (SoapObject)soapItems.getProperty(i);           
        	UserGroupVO usergroup = new UserGroupVO(); 
        	usergroup.setUsergroupid(Integer.parseInt(pii.getProperty("usergroupid").toString()));
        	usergroup.setUserid(Integer.parseInt(pii.getProperty("userid").toString()));
        	usergroup.setGroupid(Integer.parseInt(pii.getProperty("groupid").toString()));
        	usergroup.setGroupname(pii.getProperty("groupname").toString());        	    
        	usergroups[i] = usergroup;
        }
        
        int iStatusCode = Integer.parseInt(soap.getProperty("statusCode").toString());
        String sMessage = soap.getProperty("statusMessage").toString();
        
        response.setStatusCode(iStatusCode);
        response.setStatusMessage(sMessage);
        response.setUsergroups(usergroups);
        
        return response;
    }
    
    public static UserTaskListResponse RetrieveUserTaskListsFromSoap(SoapObject soap)
    {
    	UserTaskListResponse response = new UserTaskListResponse();
    	
    	SoapObject soapItems = (SoapObject)soap.getProperty("usertasklists");
    	UserTaskListVO[] usertasklists = new UserTaskListVO[soapItems.getPropertyCount()];
        for (int i = 0; i < usertasklists.length; i++) {
        	SoapObject pii = (SoapObject)soapItems.getProperty(i);           
        	UserTaskListVO usertasklist = new UserTaskListVO();            
        	usertasklist.setUserid(Integer.parseInt(pii.getProperty("userid").toString()));
        	usertasklist.setTasklistid(Integer.parseInt(pii.getProperty("tasklistid").toString()));
        	usertasklist.setTasklistname(pii.getProperty("tasklistname").toString());  
        	usertasklist.setUsergroupid(Integer.parseInt(pii.getProperty("usergroupid").toString()));
        	usertasklist.setTaskliststatusid(Integer.parseInt(pii.getProperty("taskliststatusid").toString()));
        	usertasklists[i] = usertasklist;
        }
        
        int iStatusCode = Integer.parseInt(soap.getProperty("statusCode").toString());
        String sMessage = soap.getProperty("statusMessage").toString();
        
        response.setStatusCode(iStatusCode);
        response.setStatusMessage(sMessage);
        response.setUsertasklists(usertasklists);
        
        return response;
    }
    
    public static UserTaskResponse RetrieveUserTasksFromSoap(SoapObject soap)
    {
    	UserTaskResponse response = new UserTaskResponse();
    	
    	SoapObject soapItems = (SoapObject)soap.getProperty("usertasks");
    	UserTaskVO[] usertasks = new UserTaskVO[soapItems.getPropertyCount()];
        for (int i = 0; i < usertasks.length; i++) {
        	SoapObject pii = (SoapObject)soapItems.getProperty(i);           
        	UserTaskVO usertask = new UserTaskVO();            
        	usertask.setTaskid(Integer.parseInt(pii.getProperty("taskid").toString()));
        	usertask.setName(pii.getProperty("name").toString());
        	usertask.setTasklistid(Integer.parseInt(pii.getProperty("taskstatusid").toString()));
        	usertask.setStatus(pii.getProperty("status").toString());  
        	usertask.setTasklistid(Integer.parseInt(pii.getProperty("tasklistid").toString()));        	
        	usertasks[i] = usertask;
        }
        
        int iStatusCode = Integer.parseInt(soap.getProperty("statusCode").toString());
        String sMessage = soap.getProperty("statusMessage").toString();
        
        response.setStatusCode(iStatusCode);
        response.setStatusMessage(sMessage);
        response.setUsertasks(usertasks);
        
        return response;
    }
    //
    public static UserResponse UserLogin(String useremail, String userpassword)
    {
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "userLogin";
        PropertyInfo[] pi = new PropertyInfo[2];
        pi[0] = new PropertyInfo();
        pi[0].setName("useremail");
        pi[0].setValue(useremail);
        pi[0].setType(String.class);
        pi[1] = new PropertyInfo();
        pi[1].setName("userpassword");
        pi[1].setValue(userpassword);
        pi[1].setType(String.class);
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserResponse userResponse = RetrieveUsersFromSoap(response);               
        
        return userResponse;        
    }
    
    public static UserResponse userSignUp(String useremail, String userpassword, String firstname, String lastname)
	{			
				
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "userSignUp";
        PropertyInfo[] pi = new PropertyInfo[4];
        pi[0] = new PropertyInfo();
        pi[0].setName("useremail");
        pi[0].setValue(useremail);
        pi[0].setType(String.class);
        pi[1] = new PropertyInfo();
        pi[1].setName("userpassword");
        pi[1].setValue(userpassword);
        pi[1].setType(String.class);
        pi[2] = new PropertyInfo();
        pi[2].setName("firstname");
        pi[2].setValue(firstname);
        pi[2].setType(String.class);
        pi[3] = new PropertyInfo();
        pi[3].setName("lastname");
        pi[3].setValue(lastname);
        pi[3].setType(String.class);
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserResponse userResponse = RetrieveUsersFromSoap(response);               
        
        return userResponse;        
	}
    
    public static UserGroupResponse getUserGroups(int userid)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "getUserGroups";
        PropertyInfo[] pi = new PropertyInfo[1];
        pi[0] = new PropertyInfo();
        pi[0].setName("userid");
        pi[0].setValue(userid);
        pi[0].setType(int.class);       
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserGroupResponse userGroupResponse = RetrieveUserGroupsFromSoap(response);               
        
        return userGroupResponse;        		
	}	
    
    public static UserGroupResponse createGroup(int userid, String groupname)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "createGroup";
        PropertyInfo[] pi = new PropertyInfo[2];
        pi[0] = new PropertyInfo();
        pi[0].setName("userid");
        pi[0].setValue(userid);
        pi[0].setType(int.class);
        pi[1] = new PropertyInfo();
        pi[1].setName("groupname");
        pi[1].setValue(groupname);
        pi[1].setType(String.class);
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserGroupResponse userGroupResponse = RetrieveUserGroupsFromSoap(response);               
        
        return userGroupResponse;
	}
    
    public static UserTaskListResponse getUserTaskLists(int userid)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "getUserTaskLists";
        PropertyInfo[] pi = new PropertyInfo[1];
        pi[0] = new PropertyInfo();
        pi[0].setName("userid");
        pi[0].setValue(userid);
        pi[0].setType(int.class);       
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserTaskListResponse userTaskListResponse = RetrieveUserTaskListsFromSoap(response);               
        
        return userTaskListResponse;       
	}
    
    public static UserTaskListResponse createTaskList(int usergroupid, String tasklistname)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "createTaskList";
        PropertyInfo[] pi = new PropertyInfo[2];
        pi[0] = new PropertyInfo();
        pi[0].setName("usergroupid");
        pi[0].setValue(usergroupid);
        pi[0].setType(int.class);
        pi[1] = new PropertyInfo();
        pi[1].setName("tasklistname");
        pi[1].setValue(tasklistname);
        pi[1].setType(String.class);
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserTaskListResponse userTaskListResponse = RetrieveUserTaskListsFromSoap(response);               
        
        return userTaskListResponse;
	}
    
    public static UserTaskListResponse updateTaskListStatus(int tasklistid, int taskliststatusid)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "updateTaskListStatus";
        PropertyInfo[] pi = new PropertyInfo[2];
        pi[0] = new PropertyInfo();
        pi[0].setName("tasklistid");
        pi[0].setValue(tasklistid);
        pi[0].setType(int.class);
        pi[1] = new PropertyInfo();
        pi[1].setName("taskliststatusid");
        pi[1].setValue(taskliststatusid);
        pi[1].setType(int.class);
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserTaskListResponse userTaskListResponse = RetrieveUserTaskListsFromSoap(response);               
        
        return userTaskListResponse;
	}
    
    public static UserTaskResponse getTasks(int tasklistid)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "getTasks";
        PropertyInfo[] pi = new PropertyInfo[1];
        pi[0] = new PropertyInfo();
        pi[0].setName("tasklistid");
        pi[0].setValue(tasklistid);
        pi[0].setType(int.class);       
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserTaskResponse userTaskResponse = RetrieveUserTasksFromSoap(response);               
        
        return userTaskResponse;   
	}
    
    public static UserTaskResponse creatTask(int tasklistid, String taskname)
	{
    	String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "creatTask";
        PropertyInfo[] pi = new PropertyInfo[2];
        pi[0] = new PropertyInfo();
        pi[0].setName("tasklistid");
        pi[0].setValue(tasklistid);
        pi[0].setType(int.class);  
        pi[1] = new PropertyInfo();
        pi[1].setName("taskname");
        pi[1].setValue(taskname);
        pi[1].setType(String.class);  
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserTaskResponse userTaskResponse = RetrieveUserTasksFromSoap(response);               
        
        return userTaskResponse;   
	}
	
	public static UserTaskResponse updateTaskStatus(int taskid, int taskstatusid)
	{
		String URL = "http://10.0.2.2:8080/TaskService/services/TaskService";
        String MethodName = "updateTaskStatus";
        PropertyInfo[] pi = new PropertyInfo[2];
        pi[0] = new PropertyInfo();
        pi[0].setName("taskid");
        pi[0].setValue(taskid);
        pi[0].setType(int.class);  
        pi[1] = new PropertyInfo();
        pi[1].setName("taskstatusid");
        pi[1].setValue(taskstatusid);
        pi[1].setType(int.class);  
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        UserTaskResponse userTaskResponse = RetrieveUserTasksFromSoap(response);               
        
        return userTaskResponse;  
	}
    
	public static ItemDTO[] RetrieveFromSoap(SoapObject soap)
    {
    	SoapObject soapItems = (SoapObject)soap.getProperty(0);
        ItemDTO[] items = new ItemDTO[soapItems.getPropertyCount()];
        for (int i = 0; i < items.length; i++) {
        	SoapObject pii = (SoapObject)soapItems.getProperty(i);           
            ItemDTO item = new ItemDTO();            
            item.setItemId(Integer.parseInt(pii.getProperty("itemId").toString()));
            item.setSeller(pii.getProperty("seller").toString());
            item.setTitle(pii.getProperty("title").toString());
            item.setDescription(pii.getProperty("description").toString());
            item.setPrice(Float.parseFloat(pii.getProperty("price").toString()));
            item.setAvailableqty(Integer.parseInt(pii.getProperty("availableqty").toString()));                       
            items[i] = item;
        }
        return items;
    }
	 
    public static ItemDTO[] GetItems()
    {
    	String URL = "http://10.0.2.2:8080/SimpleMarketPlaceService/services/SimpleMarketPlaceService";
        String MethodName = "getItems";
        PropertyInfo[] pi = new PropertyInfo[1];
        pi[0] = new PropertyInfo();
        pi[0].setName("userid");
        pi[0].setValue(2);
        pi[0].setType(int.class);
        SoapObject response = InvokeMethod(URL,MethodName,pi);

        return RetrieveFromSoap(response);
        
    }
    
    public static String TestPrimitiveCall()
	{
		String result = "";
		//String NAMESPACE = "http://DefaultNamespace/";
        String METHOD_NAME = "getGreetings";
        String SOAP_ACTION = "http://DefaultNamespace/getGreetings";
        String URL = "http://10.0.2.2:8080/WebServiceExample/services/HelloWorld";

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("name");
        pi.setValue("Parag");
        pi.setType(String.class);
        Request.addProperty(pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);       
        envelope.setOutputSoapObject(Request);
        
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        
        try
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            result =  response.toString();
            	         
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        return result;
	}


}
